<?php
/**
 * Created by leiying
 * Date: 2017/8/31
 * Time: 10:22
 */


    /**
     * 记录帐户变动
     * 
     * @param   int     $user_id    用户id
     * @param   float   $amount     可用余额变动
     * @param   int     $integral   等级积分变动
     * @param   int     $change_type变动类型：参见常量文件
     * @param   string  $change_desc变动说明
     * @return  void
     */
function update_user_account($user_id,$amount,$integral=0,$type=1,$change_desc=''){

        $user_account['user_id']     = $user_id;
        $user_account['user_money']  = $amount;
        $user_account['user_integral'] = $integral;
        $user_account['change_type'] = $type;
        $user_account['change_time'] = time();
        $user_account['change_desc'] = $change_desc;
        
        /* 插入帐户变动记录 */
        $res = $GLOBALS['db']->autoExecute($GLOBALS['db']->table('user_account'), $user_account, 'INSERT');
        $res_id = $GLOBALS['db']->insert_id();

        if($res_id){

            if($type==1){
                $amount = '-'.$amount;
            }

            /* 更新用户信息 */
            $sql = "UPDATE " . $GLOBALS['db']->table('users') .
                    " SET user_money = user_money + ('$amount')," .
                    " user_integral = user_integral + ('$integral')" .
                    " WHERE user_id = '$user_id' LIMIT 1";
            $GLOBALS['db']->query($sql);
        }

        return $res_id;
}

/**获取用户总积分
 * @param $uid
 * @return mixed
 * @author leiying
 */
function get_user_integral($uid){
    $sql = "SELECT `user_integral` FROM ".$GLOBALS['db']->table("users").' WHERE user_id='.$uid;
    $res = $GLOBALS['db']->getOne($sql);
    return $res;
}

/**
 * 对查询结果集进行排序
 * @access public
 * @param array $list 查询结果
 * @param string $field 排序的字段名
 * @param array $sortby 排序类型
 * asc正向排序 desc逆向排序 nat自然排序
 * @return array
 */
function list_sort_by($list,$field, $sortby='asc') {
    if(is_array($list)){
        $refer = $resultSet = array();
        foreach ($list as $i => $data)
            $refer[$i] = &$data[$field];
        switch ($sortby) {
            case 'asc': // 正向排序
                asort($refer);
                break;
            case 'desc':// 逆向排序
                arsort($refer);
                break;
            case 'nat': // 自然排序
                natcasesort($refer);
                break;
        }
        foreach ( $refer as $key=> $val)
            $resultSet[] = &$list[$key];
        return $resultSet;
    }
    return false;
}

/**
 * 把返回的数据集转换成Tree
 * @param array $list 要转换的数据集
 * @param string $pid parent标记字段
 * @param string $level level标记字段
 * @return array
 */
function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0) {
    // 创建Tree
    $tree = array();
    if(is_array($list)) {
        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] =& $list[$key];
        }
        foreach ($list as $key => $data) {
            // 判断是否存在parent
            $parentId = $data[$pid];
            if ($root == $parentId) {
                $tree[] =& $list[$key];
            }else{
                if (isset($refer[$parentId])) {
                    $parent =& $refer[$parentId];
                    $parent[$child][] =& $list[$key];
                }
            }
        }
    }
    return $tree;
}

/**
 * 将tree转换成以为数组(用于产品分类/文章分类)
 * @author leiying
 */

function tree_to_array($tree,&$list = array(),$level = 0){
    if(is_array($tree)){
        $num = $level+1;
        foreach($tree as &$v){
            if(isset($v['user_pic'])){
                $v['user_pic'] = GZ_Api::get_url($v['user_pic']);
            }
            if(isset($v['_child'])){
                $child = $v['_child'];
                unset($v['_child']);
                $v['level'] = $level;
                $list[] = $v;
                $list = tree_to_array($child,$list,$num);
            }else{
                $v['level'] = $level;
                $list[] = $v;
            }
        }
    }
    return $list;
}


/**
 * 将list_to_tree的树还原成列表
 * @param  array $tree  原来的树
 * @param  string $child 孩子节点的键
 * @param  string $order 排序显示的键，一般是主键 升序排列
 * @param  array  $list  过渡用的中间数组，
 * @return array        返回排过序的列表数组
 */
function tree_to_list($tree, $child = '_child', $order='id', &$list = array()){
    if(is_array($tree)) {
        $refer = array();
        foreach ($tree as $key => $value) {
            $reffer = $value;
            if(isset($reffer[$child])){
                unset($reffer[$child]);
                tree_to_list($value[$child], $child, $order, $list);
            }
            $list[] = $reffer;
        }
        $list = list_sort_by($list, $order, $sortby='asc');
    }
    return $list;
}





















